package com.yixin.vuehr.web.controller;

import com.yixin.vuehr.bean.RespBean;
import com.yixin.vuehr.util.HrUtils;
import com.yixin.vuehr.web.util.VerificationCode;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;

@RestController
public class LoginController {
    @GetMapping("/login")
    public RespBean login(){
        return RespBean.error("还没有登入，请登入！");
    }

    @GetMapping("/verifyCode")
    public void verifyCode(HttpSession session, HttpServletResponse resp) throws IOException {
        VerificationCode code = new VerificationCode();
        BufferedImage image = code.getImage();
        String text = code.getText();
        session.setAttribute("verify_code",text);
        VerificationCode.output(image,resp.getOutputStream());
    }

    @RequestMapping("/isAdmin")
    public Boolean isAdmin() {
        List<GrantedAuthority> authorities = (List<GrantedAuthority>) HrUtils.getCurrentHr().getAuthorities();
        for (GrantedAuthority authority : authorities) {
            if (authority.getAuthority().contains("ROLE_admin")) {
                return true;
            }
        }
        return false;
    }

//    @RequestMapping(value = "/updateUserEmail",method = RequestMethod.PUT)
//    public RespBean updateUserEmail(String email) {
//        if (userService.updateUserEmail(email) == 1) {
//            return new RespBean("success", "开启成功!");
//        }
//        return new RespBean("error", "开启失败!");
//    }
}
